package com.david.common.utils;

import com.david.system.pojo.Entity.ChildrenEntity;
import com.david.system.pojo.Entity.JobEntity;
import lombok.NoArgsConstructor;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@NoArgsConstructor
public class EntityUtils {

    public Object parStringnull2null(Object entity) {
        Class<JobEntity> jobEntityClass = JobEntity.class;
        Field[] fields = jobEntityClass.getDeclaredFields();
        Method[] methods = jobEntityClass.getMethods();
        for (Field field : fields) {
            String name = field.getName();
            String getMethod = "get"+name.substring(0,1).toUpperCase()+name.substring(1);
            Object invoke = null;
            try {
                invoke = jobEntityClass.getMethod(getMethod).invoke(entity);
            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
            if (invoke=="null" || invoke == "") {
                for (Method method : methods)
                    if (method.getName().contains("set") && method.getName().contains(name.substring(0,1).toUpperCase()+name.substring(1))){
                        try {
                            jobEntityClass.getMethod(method.getName(),String.class).invoke(entity, (Object) null);
                        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                            throw new RuntimeException(e);
                        }
                    }
            }
        }
        return entity;
    }


    public static <T extends ChildrenEntity> List<T> listToCascader(List<T> list) {
        ArrayList<T> ts = new ArrayList<>();
        System.out.println(list.toString());
        List<Integer> parentIdList0 = new ArrayList<>();
        for (T t: list) {
            parentIdList0.add(t.getParentId());
        }
        // 倒序排列
        parentIdList0.sort(Comparator.reverseOrder());
        // 去重
        List<Integer> parentIdList = parentIdList0.stream().distinct().collect(Collectors.toList());
        System.out.println(parentIdList);
        for (Integer parentId : parentIdList) {
            if (parentId.equals(0)) {

                for (T t : list) {
                    if (t.getParentId().equals(0)) {
                        System.out.println(2);
                        ts.add(t);
                    }
                }
                return ts;
            } else {
                for (T t : list) {
                    if (Objects.equals(t.getParentId(), parentId)) {
                        for (T t1 : list) {
                            if (Objects.equals(t1.getId(), parentId)) {
                                t1.getChildren().add(t);
                            }
                        }
                    }
                }
            }
        }
//        System.out.println(1);
//        ArrayList<T> ts = new ArrayList<>();
//        for (T t : list) {
//            if (t.getParentId().equals(0)) {
//                System.out.println(2);
//                ts.add(t);
//            }
//        }
//        System.out.println("ts"+ts.toString());
        return ts;
    }
}
